#coding=utf-8
import wx, threading, Queue, time, sys
from fetcher import google_cn as f

def add_az(query):
    r = [query, ]
    az = [chr(i)for i in range(97,123)]
    r.extend([' '.join([query, i]) for i in az])
    return r
    
class QueryRecorder(object):
    def __init__(self):
        self.list = []
    
    def has(self, item):
        if item in self.list:return True
        return False
        
    def add(self, item):
        if not self.has(item): self.list.append(item)
    
    def get(self):
        return self.list
                
class FetcherThread(threading.Thread):

    def __init__(self, fetcher, query_queue, result):
        threading.Thread.__init__(self)
        
        self.fetcher = fetcher
        
        self.query_queue = query_queue
        self.result = result
    
    def run(self):
        while True:
            q = self.query_queue.get()
            print 'Querying:%s'%q
            data = self.fetcher(q)
            if not data:
            
                self.query_queue.task_done()
                continue
                
            for d in data:
            
                print d
                self.result.append(d)
                if not recorder.has(d):
                    recorder.add(d)
                    self.query_queue.put(d)
                    
            if len(data) >= 10 and len(q.split(' ')[-1]) != 1:
                
                for d in add_az(q):
                    if not recorder.has(d):
                        recorder.add(d)
                        self.query_queue.put(d)
            
            self.query_queue.task_done()
        time.sleep(0.05)
              
query = sys.argv[1]

thread_num = 100

query_queue = Queue.Queue()
result = []

recorder = QueryRecorder()

for d in add_az(query):
    query_queue.put(d)
    recorder.add(d)
    
for i in range(thread_num):
    t = FetcherThread(f, query_queue, result)
    t.setDaemon(True)
    t.start()

query_queue.join()
print len(set(result))
outfile = open('./%s.txt'%query, 'w')
for d in set(result):
    outfile.write(d.encode('gbk'))
    outfile.write('\r\n')
    
outfile.close()

